﻿using System;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.WebControl;

namespace ActivitySystem.Web.Admin
{
    public partial class RoleList : WebPointPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    Search();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                gvRoles.PageIndex = 0;
                Search();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvRoles.PageIndex = e.NewPageIndex;
                Search();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        private void Search()
        {
            string roleName = tbxRoleName.Text;
            bool isActive = string.IsNullOrEmpty(ddlStatus.SelectedValue) ? true : Boolean.Parse(ddlStatus.SelectedValue);

            RoleManager roleManager = new RoleManager();

            if (gvRoles.PageIndex == 0)
            {
                gvRoles.TotalCount = roleManager.SearchCount(roleName, isActive);
            }

            gvRoles.DataSource = roleManager.Search(roleName, isActive, gvRoles.PageIndex, gvRoles.PageSize);
            gvRoles.DataBind();

            if (!Page.IsPostBack)
            {
                AuditLogger.Write(AuditLogOperationCode.Search, this.GetType().Name, roleName, isActive, gvRoles.PageIndex, gvRoles.PageSize);
            }
        }

        protected string BindSystemName(Guid systemId)
        {
            string systemName=string.Empty;

            var systemInfoManager = new SystemInfoManager();
            var systemInfo = systemInfoManager.Select(systemId);

            if (systemInfo != null) systemName = systemInfo.SystemName;

            return systemName;
        }

        #region WebPointPage Member

        protected override void ApplyPermission()
        {
            if (!this.HasPermission(PermissionType.Create))
            {
                litCreateNew.Visible = false;
            }

            if (!this.HasPermission(PermissionType.Update))
            {
                if (gvRoles.Columns.Count >= 1) gvRoles.Columns[gvRoles.Columns.Count - 1].Visible = false;
            }
        }

        #endregion
    }
}